#include "http-session-on-tcp.h"

#include "util/logger/logger.h"

#include "http-server-on-tcp.h"


const char *name() {
	return "HttpSessionOnTCP ";
}

HttpSessionOnTCP::HttpSessionOnTCP(HttpServer &server, tcp::socket &&socket)
	: HttpSession(server)
	, socket_(std::move(socket))
{
	LOGD << name() << "(" << this << ") constructed,"
		<< "remote: " << socket_.remote_endpoint().address() << ":" << socket_.remote_endpoint().port();

	startHandleMessage();
}

HttpSessionOnTCP::~HttpSessionOnTCP()
{
}

void HttpSessionOnTCP::asyncReadHttpRequest(UnderRequestBuffer &requestBuffer, UnderRequest &request, std::function<void(std::error_code ec, size_t readn)> &&cb)
{
	boost::beast::http::async_read(socket_, requestBuffer, request, std::move(cb));
}

void HttpSessionOnTCP::asyncWriteHttpResponse(const UnderResponse &response, std::function<void(std::error_code ec, size_t readn)> &&cb)
{
	boost::beast::http::async_write(socket_, response, std::move(cb));
}
